<?php include_once("../config.php"); ?>


<?



function removeHtml($content)
{
    $handle1  = htmlspecialchars_decode($content);  //把一些预定义的 HTML 实体转换为字符
    $handle2  = str_replace("&nbsp;", "", $handle1);//将空格替换成空
    $contents = strip_tags($handle2);               //函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容

    return  $contents;        //返回字符串中的前100字符串长度的字符
}





function send_wx_sms($url , $info)
{
    $res =  request_post($url, $info);
    return $res;
}



function request_post($url = '', $post_data = array()) {
    if (empty($url) || empty($post_data)) {
        return false;
    }

    $o = "";
    foreach ( $post_data as $k => $v )
    {
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);

    $postUrl = $url;

    $curlPost = $post_data;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}

















//开始逐个网址访问，获取最新的代码，对比上一次的代码
//开始逐个网址访问，获取最新的代码，对比上一次的代码




//获取目标网址的代码内容
$cheack_url=select_one_line_all_items($table_Prefix."monitor_list","date like '%".date("Y-m-d")."%'");

$checkurl=$cheack_url['url'];


if(!$cheack_url['url'])
{
    echo "没有网站需要监测了";
    exit;
}

//$cheack_url['url']="https://www.pecold.com";
//$cheack_url['url']="http://www.eva-yy.com/admin/";

var_dump(strstr($cheack_url['url'],"https"));



if(strstr($cheack_url['url'],"https"))
{
    echo "https mod";
    $_now_source=curl_get_url_content($cheack_url['url']);
}
else
{
    echo "file get content mod";
    $_now_source=curl_get_url_content($cheack_url['url']);
}




$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
                 "'<style[^>]*?>.*?</style>'si",  // 去掉 css
                 "'<[/!]*?[^<>]*?>'si",      // 去掉 HTML 标记
                 "'<!--[/!]*?[^<>]*?>'si",      // 去掉 注释 标记
                 "'([rn])[s]+'",  // 去掉空白字符
                 "'&(quot|#34);'i",  // 替换 HTML 实体

                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e");   // 作为 PHP 代码运行

$replace = array ("",
                  "",
                  "",
                  "",
                  "\1",
                  "\"",
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\1)");
//$document为需要处理字符串，如果来源为文件可以$document = file_get_contents($filename);
$_now_source = preg_replace($search, $replace, $_now_source);



//提取文字
//$preg = "/<script[\s\S]*?<\/script>/i";
//$_now_source = preg_replace($preg,"",$_now_source,-1);    //第四个参数中的3表示替换3次，默认是-1，替换全部
//
//
//$_now_source=strip_tags($_now_source);

//这里不能同一行，同一行则会失效
$_now_source = preg_replace( "/
|\n/", "", $_now_source );



$_now_source=addslashes($_now_source);
$_now_source=strtr($_now_source," ","");


print_r2($cheack_url['url']);
echo "<br/>";
//当前获取到的数据
echo "当前获取到的数据<br/>";
echo $_now_source;
echo "<br/>";
echo "<br/>";


//获取上次的，上上次的，上上上次的网站的内容
$_contents=select_one_line_all_items($table."monitor_log","url = '".$cheack_url['url']."'");

echo "数据库中的旧数据<br/><br/>";
print_r2($_contents);
echo "<br/><br/>";


//如果不存在数据，则先插入
if(!$_contents['url'])
{
   $_creat_data['ai_url']=$cheack_url['url'];
   $_creat=insert_date($table."monitor_log",$_creat_data,"");
   $query = mysql_query($_creat) or die ("error:".mysql_error()); ;

}




if(count($_now_source) == count($_contents['content_newest']))
{
 $compare_result="无变化";

}
elseif(!$_now_source)
    {$compare_result="变化";}
else
{
 $compare_result="变化";
}


//比较完之后，就应该轮级插入。  这次结果放到news  news放到lasttime

$_data['ai_date']=date("Y-m-d h:i:s");
$_data['ai_content_newest']=$_now_source;
$_data['ai_content_lasttime']=addslashes($_contents['content_newest']);
$_data['ai_content_last2time']=addslashes($_contents['content_lasttime']);
$_data['ai_status']=$compare_result;

echo "本轮数据<br/><br/>";
print_r2($_data);
echo "<br/><br/>";



$sql=update_date($table_Prefix."monitor_log",$_data,"url",$cheack_url['url']);
$query = mysql_query($sql) or die ("error:".mysql_error().$sql);

echo "<br/><br/>";





//删除monitor_list的数据 ，让下一轮执行的可以不会重复

echo  $del_str="delete from ".$table_Prefix."monitor_list where url = '".$cheack_url['url']."'";
$query = mysql_query($del_str) or die ("error:".mysql_error()); ;


//$compare_result="变化";


//如果变化，就发信通知

if($compare_result=="变化")
{

    $emp[0]['openid']="oUaqG0dZdR4ITehiwDOLoCxcn49w";//yuan
    $emp[1]['openid']="oUaqG0efzXfqCDuI3kpPmZVjZ-";//kate
    $emp[2]['openid']="oUaqG0dLyjB9Pcv3Qm2CfKOBD69s";//ming
    $emp[3]['openid']="oUaqG0fcnwhCtByyNKj-XeBBHRkE";//lin

    print_r2($emp);

//循环发送微信通知
    foreach ($emp as $k=>$v)
    {
        include_once ROOT_PATH_."/api/wechat_message/notice_website_change.php" ;
    }



}

else
{

    echo "<br/>";
    echo "没变化";
    echo "<br/>";

}





 ?>

 